package com.direction.hold.ui;

import java.util.Collections;
import java.util.HashMap;

import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cn.cheln.explorer.actions.EqualCheck;
import cn.cheln.explorer.actions.OperationUtil;
import cn.cheln.explorer.actions.sort.DBSort;
import cn.cheln.explorer.actions.sort.ObjectSort;
import cn.cheln.explorer.actions.sort.Sort;
import cn.cheln.explorer.config.Constants;
import cn.cheln.explorer.cpnt.adapter.FileItemSrc;
import cn.cheln.explorer.ds.MediaArgs;
import cn.cheln.explorer.ds.database.CategoryInfo;
import cn.cheln.explorer.ds.database.MimeSrc;
import cn.cheln.explorer.ds.uri.AccountNode;
import cn.cheln.explorer.ds.uri.NaviSourceIndicator;
import cn.cheln.explorer.ds.uri.NavigationNode;
import cn.cheln.explorer.ui.FileListViewHolder;
import cn.cheln.explorer.ui.Switcher;
import cn.cheln.explorer.ui.ViewHolder;
import cn.cheln.support.data.text.xml.SPUtil;
import cn.cheln.support.manager.ActivityPool;
import cn.cheln.support.ui.adapter.MapAdapter.AdaptInfo;
import cn.power.shine.R;

public abstract class FileCursorActivity extends CommonActivity implements
		NaviSourceIndicator {

	public AdaptInfo getMusicAdaptInfo() {
		if (musicAdaptInfo == null) {
			musicAdaptInfo = new AdaptInfo();
			musicAdaptInfo.objectFields = new String[] {
					Constants.FieldConstants.NAME,
					// , //version_cond
					Constants.MusicInfo.ALBUM, // size
					Constants.MusicInfo.ALBUMARTIST, // versionName
					Constants.FieldConstants.ICON_RES
			// Constants.FieldConstants.CHECKBOX_OPTION
			};
			musicAdaptInfo.viewIds = new int[] { R.id.fileNameSubFileNum,
					R.id.album,
					R.id.artist,
					R.id.fileIcon
			// R.id.handleCheckbox
			};
			musicAdaptInfo.listviewItemLayoutId = R.layout.musiclistitem;
			// musicAdaptInfo.actionListeners = viewHandlers;
		}
		return musicAdaptInfo;
	}

	@Override
	public View getPageView() {
		// TODO Auto-generated method stub
		return super.getPageView();
	}

	@Override
	public void setPageView(View list) {
		// TODO Auto-generated method stub
		super.setPageView(list);
	}

	public ViewHolder loadFileCursor() {
		return loadViewHolder(FileCursorActivity.class.getName(),
				new FileListViewHolder(list, this));
	}

	public ViewHolder getViewHolder() {
		return this.holderMap.get(FileCursorActivity.class.getName());
	}

	public ViewHolder getViewHolder(String flag) {
		return this.holderMap.get(flag);
	}

	public ViewHolder newViewHolderInstance() {
		// TODO Auto-generated method stub
		return new FileListViewHolder(list, this);
	}

	public void setViewHolder(ViewHolder viewHolder) {
		this.viewHolder = viewHolder;
	}

	public void gotoCat() {
		Switcher.viewFlowTo(loadFileCursor(), contentView, this);
	}

	public HashMap<String, Class> datamap;

	public NavigationNode getNaviSource() {
		return navTool.navEntity.peek();
	}

	@Override
	protected void setHiddenFileShowFlag() {
		// TODO Auto-generated method stub

	}

	@Override
	public void handlerRead(Object previousSource, boolean startRoot) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onMenuItemClick(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onMenuItemClick(item);
	}

	@Override
	public void startup() {
		// TODO Auto-generated method stub

	}

	@Override
	public void access(NavigationNode navig, int newPos,
			boolean notifyDataSetChanged, int mode, int status) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void access(NavigationNode navig, int newPos,
			boolean notifyDataSetChanged, int mode, int status, int accessFlag) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setNavigationPosAndNode(int newPos, int mode,
			NavigationNode navSource) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public String getNavBarWholeShowText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int[] getMenuIds() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected boolean navigate(MenuItem item) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void cutOrCopy(int command) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sort() {
		// TODO Auto-generated method stub

	}

	@Override
	protected String getEmptyText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void swithToPrivacyActivity() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onStart() {
		super.onStart();
		if (policy == null) {
			policy = new DBSort(Sort.SORT_POLICY_TITLE, Sort.SORT_SEQ_ASC);
		}
		this.loadFileCursor();

	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		initData();
	}

	public boolean performCountActivityCopy(int command)
			throws NotFoundException {
		// TODO Auto-generated method stub
		EqualCheck nc = new EqualCheck(ObjectSort.SORT_POLICY_TITLE,
				ObjectSort.SORT_SEQ_ASC);
		Collections.sort(OperationUtil.selectedEntities, nc);
		if (nc.isResult()) {
			Toast.makeText(context, R.string.same_name_files_operation_cancel,
					Toast.LENGTH_LONG).show();
			return true;
		}
		this.adapter.markVisible(false);
		SlideActivity.paramActivity.isInOperation = command;
		showBottomBar(View.GONE);
		PathActivity.activityInstance.switchToCutCopy();
		SlideActivity.paramActivity.setPage(1);
		ActivityPool.getInstance().refresh();
		return false;
	}

	private void initData() {
		if (datamap == null) {
			datamap = new HashMap<String, Class>();
			datamap.put(MediaArgs.PATH, String.class);
			datamap.put(MediaArgs.TITLE, String.class);
			datamap.put(MediaArgs.COUNT, Integer.class);
			datamap.put(MediaArgs.SIZE, Integer.class);
		}

	}

	public ViewHolder viewHolder;
	public AdaptInfo apkAdaptInfo;
	public AdaptInfo musicAdaptInfo;
	public static final int SortMenuInAPK = 0;
	public static final int SortMenuInMusic = 1;
	public int sortMenuMarkInit = -1;

	public AdaptInfo getApkAdaptInfo() {
		if (apkAdaptInfo == null) {
			apkAdaptInfo = new AdaptInfo();
			apkAdaptInfo.objectFields = new String[] {
					Constants.ApkInfo.VERSION_CONDITION, // version_cond
					Constants.FieldConstants.SIZE, // size
					Constants.ApkInfo.VERSION_NAME, // versionName
					Constants.FieldConstants.NAME,
					Constants.FieldConstants.ICON_RES,
					Constants.FieldConstants.CHECKBOX_OPTION };
			apkAdaptInfo.listviewItemData = new FileItemSrc();
			apkAdaptInfo.viewIds = new int[] { R.id.versioncondition, // version_cond
					R.id.apkFileSize, // versionCode
					R.id.fileTimeSize, // size
					R.id.fileNameSubFileNum, R.id.fileIcon, R.id.handleCheckbox };
			apkAdaptInfo.listviewItemLayoutId = R.layout.filelist_item;
			apkAdaptInfo.actionListeners = viewHandlers;
		}
		return apkAdaptInfo;
	}

	// public AdaptInfo getMusicAdaptInfo() {
	// if (musicAdaptInfo == null) {
	// musicAdaptInfo = new AdaptInfo();
	// musicAdaptInfo.objectFields = new String[]{
	// Constants.MusicInfo.NAME, //version_cond
	// // Constants.MusicInfo.ALBUM, //size
	// Constants.MusicInfo.ALBUMARTIST, //versionName
	// Constants.FieldConstants.ICON_RES,
	// Constants.FieldConstants.CHECKBOX_OPTION
	// };
	// musicAdaptInfo.viewIds = new int[]{
	// R.id.fileNameSubFileNum,
	// // R.id.album,
	// R.id.artist,
	// R.id.fileIcon,
	// R.id.handleCheckbox
	// };
	// musicAdaptInfo.listviewItemLayoutId = R.layout.musiclistitem;
	// musicAdaptInfo.actionListeners = viewHandlers;
	// }
	// return musicAdaptInfo;
	// }
	protected OnClickListener categoryClickListener = new OnClickListener() {

		public void onClick(View view) {
			String mimeprefix = null;
			switch (((View) view.getParent()).getId()) {
			case R.id.audio:
				mimeprefix = Constants.CateContants.CATE_MUSIC;
				break;
			case R.id.image:
				mimeprefix = Constants.CateContants.CATE_IMAGES;
				break;
			case R.id.video:
				mimeprefix = Constants.CateContants.CATE_VIDEO;
				break;
			case R.id.doc:
				mimeprefix = Constants.CateContants.CATE_DOCS;
				break;
			case R.id.apk:
				mimeprefix = Constants.CateContants.CATE_PACKAGE;
				break;
//			case R.id.theme:
//				mimeprefix = Constants.CateContants.CATE_THEME;
//				break;
			}
			CategoryInfo cateInfo = MimeSrc.categoryRepository.get(mimeprefix).categoryInfo;
			String flag = cateInfo.categorySign;
			String displayname = cateInfo.displayName;
			navTool.navEntity.push(new AccountNode(flag, displayname));
			gotoCat();
		}
	};

	@Override
	protected void setSort() {
		// TODO Auto-generated method stub
		super.setSort();
		if (policy == null) {
			policy = new DBSort(Sort.SORT_POLICY_TITLE, Sort.SORT_SEQ_ASC);
		}
	}

	protected static boolean isFirstLaunched = true;

	public void recordPageSharedPreference(String rememberedCategory) {
		SPUtil.putValue(context,
                Constants.SharedPrefernce.RememberedCategory,
                Constants.SharedPrefernce.KEY_CATEGORY, rememberedCategory);
	}
}
